import comtypes
from comtypes import GUID

class WASAPIDevice:
    def __init__(self, device_guid: GUID):
        self.device = comtypes.CoCreateInstance(
            device_guid,
            interface=ctypes.POINTER(IAudioClient)
        )

    def set_format(self, sample_rate: int, channels: int):
        """设置音频格式"""
        format = WAVEFORMATEX()
        format.wFormatTag = WAVE_FORMAT_PCM
        format.nChannels = channels
        format.nSamplesPerSec = sample_rate
        self.device.Initialize(AUDCLNT_SHAREMODE_SHARED, 0, 10000000, 0, byref(format), None)

    def get_buffer(self, size: int) -> ctypes.Array:
        """获取缓冲区"""
        return (ctypes.c_byte * size)()